/**
 * 
 */
package tracksystem;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import managesystem.Buddy;
import managesystem.Student;
import managesystem.StudentManager;
import controlsystem.BadgeController;
import controlsystem.Course;

/**
 * @author Incalza Dario
 *
 */
public class mobileSchoolServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Buddy> budObjs = new ArrayList<Buddy>();
	private Calendar startDate;
	private Calendar stopDate;
	private String startDatestr;
	private String stopDatestr;

	public void doPost(HttpServletRequest req, HttpServletResponse resp){
		startDate = Calendar.getInstance();
		stopDate = Calendar.getInstance();
		HttpSession session = req.getSession();
		Student currStud = (Student) session.getAttribute("currStud");
		
		/*
		 * Haal alle informatie op.
		 */
		String type = (String) req.getParameter("type");
		String course = (String) req.getParameter("course");
		startDatestr = (String) req.getParameter("startDate");
		stopDatestr = (String) req.getParameter("stopDate");
		String dateStr = (String) req.getParameter("mydate");
		String place = (String) req.getParameter("place");
		String[] distractions = (String[]) req.getParameterValues("distraction");
		String[] aidTools = (String[]) req.getParameterValues("aidtools");
		String[] buddies = (String[]) req.getParameterValues("buddies");
		int rating = Integer.valueOf(req.getParameter("rating"));
		String description = (String) req.getParameter("description");
		String comment = (String) req.getParameter("comment");
		int pages = Integer.valueOf(req.getParameter("pages"));	
		
		if(type==null || course==null ||startDatestr ==null || stopDatestr ==null){
			try {
				session.setAttribute("error", "You didn't fill in all the required fields.");
				resp.sendRedirect("/index-mobile.jsp");
			} catch (IOException e) {
				
				e.printStackTrace();
			}
		}else{
			/**
			 * Associeer buddy id's met een buddy object
			 */
			
			
			
			if(buddies != null){
				parseBuddies(buddies,currStud);
			}
			
			/**
			 * Genereer een Dates van strings
			 */
			if(dateStr!=null){
			stringToDate(dateStr);}
			stringToStartDate(startDatestr);
			boolean midnight = checkEarlier(startDatestr,stopDatestr);
			stringToStopDate(stopDatestr,midnight);					
				
		
				/**
				 * Haal een course object op bij een bepaalde course naam.
				 */
				Course c = getCourseObj(course,currStud);
				/**
				 * Maak de school activity aan.
				 */
				SchoolActivity sa = new SchoolActivity(startDate.getTime(),stopDate.getTime(),place,description,rating,type,comment,c,pages);
				if(aidTools != null){
					sa.setAids(aidTools);
				}
				if(distractions != null){
					sa.setDistractions(distractions);
				}
				if(budObjs != null){
					sa.setBuddies(budObjs);
				}
				
				/**
				 * Persisteren
				 */
				currStud.addMobileActivity(sa);
				StudentManager.add(currStud);
				session.setAttribute("currStud",currStud);
				/**
				 * Doorsturen en checken op badges.
				 */
				if(!BadgeController.checkBadge(c, currStud).isEmpty()) {
					session.setAttribute("message","School activity tracked succesfully, you've also received a new badge, these badges can be viewed on your laptop in the dashboard");
				}
				session.setAttribute("message","School activity tracked succesfully.");
				try {
					resp.sendRedirect("/mobile-inc/activities.jsp");
				} catch (IOException e) {
					e.printStackTrace();
				}
			
		}
	}
	/**
	 * koppel buddy objecten aan een id.
	 * @param ids
	 * @param currStud
	 */
	private void parseBuddies (String[] ids, Student currStud){
		
		List<Buddy> studBuddies = currStud.getBuddies();
		for(int i = 0; i<ids.length;i++){
			for(Buddy b:studBuddies){
				if(Integer.valueOf(ids[i])==b.getId()){
					budObjs.add(b);
				}
			}
		}
	}
	
	/**
	 * Genereer StartDate van string
	 * @param String (hh:mm)
	 */
	private void stringToDate(String dateString){
		
		String[] dates = dateString.split("-");
		startDate.set(Calendar.YEAR,Integer.valueOf(dates[0]));
		startDate.set(Calendar.DAY_OF_MONTH,Integer.valueOf(dates[1]));
		startDate.set(Calendar.MONTH,Integer.valueOf(dates[2]));
		stopDate.set(Calendar.YEAR,Integer.valueOf(dates[0]));
		stopDate.set(Calendar.DAY_OF_MONTH,Integer.valueOf(dates[1]));
		stopDate.set(Calendar.MONTH,Integer.valueOf(dates[2]));
	}
	
	
	/**
	 * Genereer StartDate van string
	 * @param String (hh:mm)
	 */
	private void stringToStartDate(String dateString){
		String[] dates = dateString.split(":");
		startDate.set(Calendar.HOUR_OF_DAY,Integer.valueOf(dates[0]));
		startDate.set(Calendar.MINUTE,Integer.valueOf(dates[1]));
	}
	
	/**
	 * Genereer StopDate van string
	 * @param String (hh:mm)
	 */
	private void stringToStopDate(String dateString, boolean pastMidnight){
		String[] dates = dateString.split(":");
		
		if(pastMidnight){
			stopDate.add(Calendar.DATE, 1);
		}	
		stopDate.set(Calendar.HOUR_OF_DAY,Integer.valueOf(dates[0]));
		stopDate.set(Calendar.MINUTE,Integer.valueOf(dates[1]));		
	}
	
	/**
	 * Haal een course object op , door middel van een courseName.
	 * @param courseName
	 * @param currStud
	 * @return
	 */
	private Course getCourseObj(String courseName, Student currStud){
		Course c = null;
		List<Course> studCourses = currStud.getCourses();
		for(Course tempc : studCourses){
			if(tempc.getName().equals(courseName)){
				c = tempc;
				break;
			}
		}
		return c;
	}
	/**
	 * Kijk of stopDate vroeger is dan startDate , zoja is middernacht gepasseerd.
	 * @param start
	 * @param stop
	 * @return middernacht gepasseerd of niet.
	 */
	private boolean checkEarlier(String start, String stop){
		String[] startData = start.split(":");
		String[] stopData = stop.split(":");
		if(Integer.valueOf(startData[0]) > Integer.valueOf(stopData[0])){
			return true;
		}
		else{
			return false;
		}
				
	}

}
